---
description: "Reference for Nitric's v0 JVM library - Returns a reference to the latest version of a secret, regardless of that version's ID."
---

# JVM - secret.latest()

Returns a reference to the `latest` version of a secret, regardless of that version's ID.

<Tabs syncKey="jvm-lang">

<TabItem label="Java">

```java
import io.nitric.Nitric;
import io.nitric.resources.SecretPermission;

public class Application {
  public static void main(String[] args) {
    var secrets = Nitric.INSTANCE.secret("encryptionKey").with(SecretPermission.Access);

    var latestRef = secrets.latest();

    Nitric.INSTANCE.run();
  }
}
```

</TabItem>

<TabItem label="Kotlin">

```kotlin
import io.nitric.Nitric
import io.nitric.resources.SecretPermission

fun main() {
  val secrets = Nitric.secret("encryptionKey").with(SecretPermission.Access)

  val latestRef = secrets.latest()

  Nitric.run()
}
```

</TabItem>

</Tabs>

## Notes

`latest()` is most useful when you always want the most recent secret values from the secrets manager. Database credentials and API keys are good examples of secrets where the latest value is usually what you want.

For symmetric encryption, you'll need to retrieve the version of the secret used to _encrypt_ a value when you try to _decrypt_ it again. In those cases `latest()` isn't a good choice, use [version()](./secret-version) instead.

## Examples

### Get a reference to the latest secret version

<Tabs syncKey="jvm-lang">

<TabItem label="Java">

```java
import io.nitric.Nitric;
import io.nitric.resources.SecretPermission;

public class Application {
  public static void main(String[] args) {
    var secrets = Nitric.INSTANCE.secret("encryptionKey").with(SecretPermission.Access);

    var latestRef = secrets.latest();

    Nitric.INSTANCE.run();
  }
}
```

</TabItem>

<TabItem label="Kotlin">

```kotlin
import io.nitric.Nitric
import io.nitric.resources.SecretPermission

fun main() {
  val secrets = Nitric.secret("encryptionKey").with(SecretPermission.Access)

  val latestRef = secrets.latest()

  Nitric.run()
}
```

</TabItem>

</Tabs>

### Access the latest value of a secret

<Tabs syncKey="jvm-lang">

<TabItem label="Java">

```java
import io.nitric.Nitric;
import io.nitric.resources.SecretPermission;

public class Application {
  public static void main(String[] args) {
    var secrets = Nitric.INSTANCE.secret("encryptionKey").with(SecretPermission.Access);

    var key = secrets.latest().access();

    Nitric.INSTANCE.run();
  }
}
```

</TabItem>

<TabItem label="Kotlin">

```kotlin
import io.nitric.Nitric
import io.nitric.resources.SecretPermission

fun main() {
  val secrets = Nitric.secret("encryptionKey").with(SecretPermission.Access)

  val key = secrets.latest().access()

  Nitric.run()
}
```

</TabItem>

</Tabs>

<Note>
  See [secret.version().access()](./secret-version-access) for more details.
</Note>
